package com.chanjiedata.changbs.module.mine;

/**
 * 文件名 : BindBankCardActivity
 * 创建者 : MangoWang
 * 创建日期 : 2019/11/27 18:11
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.fragment.app.DialogFragment;

import com.chanjiedata.changbs.R;
import com.chanjiedata.changbs.base.BaseActivity;
import com.chanjiedata.changbs.base.MainApplication;
import com.chanjiedata.changbs.help.UserHelper;
import com.chanjiedata.changbs.net.Apis;
import com.chanjiedata.changbs.net.NetworkManager;
import com.chanjiedata.changbs.net.interfaces.OnRequestCallBack;
import com.chanjiedata.changbs.proto.Connect;
import com.chanjiedata.changbs.proto.MemberBank;
import com.example.havi.util.StatusBarUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import kale.ui.view.dialog.EasyDialog;

public class BindBankCardActivity extends BaseActivity {


    @BindView(R.id.back_tv)
    TextView backTv;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_bank_card_number)
    EditText etBankCardNumber;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_mobile_code)
    EditText etMobileCode;
    @BindView(R.id.get_code_tv)
    TextView getCodeTv;
    @BindView(R.id.btn_submit)
    Button btnSubmit;
    private String mPhone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_bank_card);
        StatusBarUtil.setStatusBarLightMode(this, Color.parseColor("#ffffff"), true);
        ButterKnife.bind(this);
        initListener();
        etName.setText(UserHelper.getUser().getTruename());
        tvTitle.setText("绑定银行卡");
    }

    private void initListener() {
        backTv.setOnClickListener(view -> {
            EasyDialog.Builder builder = EasyDialog.builder(this); // 建立builder对象
            builder.setTitle("提示")
                    .setMessage("是否放弃绑定银行卡")
                    .setOnCancelListener(dialogInterface -> {

                    })
                    .setOnDismissListener(dialogInterface -> {

                    })
                    // 设置下方的三个按钮
                    .setPositiveButton("确定", (dialog, which) -> {
                        MainApplication.finishTopActivity(2);
                    })
                    .setNegativeButton("取消", (dialog, which) -> dialog.dismiss())
                    .setCancelable(true); // 点击空白处可以关闭

            DialogFragment easyDialog = builder.build();

// 用showAllowingStateLoss()弹出
            easyDialog.show(getSupportFragmentManager(), "");
        });
        getCodeTv.setOnClickListener(view -> {
            getCode();
        });
        btnSubmit.setOnClickListener(view -> {
            sumbit();
        });
    }


    /**
     * 获取验证码
     */
    private void getCode() {
        mPhone = etPhone.getText().toString();
        if (TextUtils.isEmpty(mPhone)) {
            showToast("请输入手机号码");
            return;
        }
        Connect.captcha.Builder builder = Connect.captcha.newBuilder();
        builder.setPhone(mPhone);
        builder.setType("5");
        Connect.captcha data = builder.build();
        showProgress();
        NetworkManager.INSTANCE.post(Apis.getMobileCode, data.toByteArray(), new OnRequestCallBack() {
            @Override
            public void onOk(byte[] bytes) {
                hideProgress();
            }

            @Override
            public void onError(int code, String errorMessage) {
                hideProgress();
                showToast(errorMessage);
                if (code == 200) {
                    countDownReSend(getCodeTv, 60);
                }
            }
        });
    }

    private void sumbit() {
        String account = etName.getText().toString();
        if (TextUtils.isEmpty(account)) {
            showToast("请输入持卡人姓名");
            return;
        }
        String CardNumber = etBankCardNumber.getText().toString();
        if (TextUtils.isEmpty(CardNumber)) {
            showToast("请输入银行卡号");
            return;
        }


        String phone = etPhone.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            showToast("请输入预留手机");
            return;
        }

        String MobileCode = etMobileCode.getText().toString();
        if (TextUtils.isEmpty(MobileCode)) {
            showToast("请输入验证码");
            return;
        }
        MemberBank.bank.Builder builder = MemberBank.bank.newBuilder();
        builder.setBankCard(CardNumber)
                .setUserName(account)
                .setPhone(phone).
                setCaptcha(MobileCode);
        showProgress();
        NetworkManager.INSTANCE.post(Apis.bank_card_add, builder.build().toByteArray(), new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) {

            }

            @Override
            public void onError(int code, String errorMessage) {
                hideProgress();
                if (code == 200) {
                    showToast(errorMessage);
                    MainApplication.finishTopActivity(2);
                } else {
                    showHintDialog(errorMessage);
                }
            }
        });
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {

            EasyDialog.Builder builder = EasyDialog.builder(this); // 建立builder对象
            builder.setTitle("提示")
                    .setMessage("是否放弃绑定银行卡")
                    .setOnCancelListener(dialogInterface -> {

                    })
                    .setOnDismissListener(dialogInterface -> {

                    })
                    // 设置下方的三个按钮
                    .setPositiveButton("确定", (dialog, which) -> {
                        MainApplication.finishTopActivity(2);
                    })
                    .setNegativeButton("取消", (dialog, which) -> dialog.dismiss())
                    .setCancelable(true); // 点击空白处可以关闭

            DialogFragment easyDialog = builder.build();

// 用showAllowingStateLoss()弹出
            easyDialog.show(getSupportFragmentManager(), "");
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


}
